package com.rp.system.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleTrigger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.JobDetailBean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.rp.common.utils.RpUtil;
import com.rp.system.dao.TaskDao;
import com.rp.system.po.Task;

@Service
@Transactional
public class TaskService {

	@Autowired
	private TaskDao taskDao;

	/*@Resource(name = "quartzScheduler")
	private Scheduler scheduler;
	
	*//**
	 * 检查任务名称是否存在
	 * @param taskName
	 * @return
	 *//*
	public boolean checkTaskNameExist(String taskName){
		List<Task> tasks = taskDao.find("from Task where taskName = ?", taskName);
		if(tasks.size() > 0){
			return true;
		}
		return false;
	}

	*//**
	 * 添加任务
	 * 
	 * @param task
	 * @throws Exception 
	 * @throws SchedulerException 
	 * @throws ClassNotFoundException
	 *//*
	public void addTask(Task task) throws ClassNotFoundException, SchedulerException {
		try{
			Class<?> jobClass = Class.forName(task.getJobClass());
	
			JobDetailBean jobDetail = new JobDetailBean();
			jobDetail.setName(task.getTaskName());
			jobDetail.setJobClass(jobClass);
	
			SimpleTrigger trigger = new SimpleTrigger(RpUtil.createGuid());
			Date startTime = RpUtil.parseDate(task.getStartTime(), null);
			Date endTime = RpUtil.parseDate(task.getEndTime(), null);
	
			trigger.setStartTime(startTime);
			trigger.setEndTime(endTime);
			trigger.setRepeatCount(-1);//设置无限次重复
			trigger.setRepeatInterval(Long.parseLong(task.getIntervalTime()));
			
			//scheduler.addTriggerListener(new MyTriggerListener());
			//scheduler.addJobListener(new MyJobListener());
			
			//trigger.addTriggerListener("MyTriggerListener");
			//jobDetail.addJobListener("MyJobListener");
			
			scheduler.scheduleJob(jobDetail, trigger);
			if(!this.isStarted()){
				scheduler.start();
			}
			
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			throw e;
		} catch (SchedulerException e) {
			e.printStackTrace();
			throw e;
		}
		
		task.setTaskStatus(Task.RUNNING);
		taskDao.save(task);
		
	}

	*//**
	 * 启动任务调度器
	 * 
	 * @throws SchedulerException
	 *//*
	public void start() throws SchedulerException {
		scheduler.start();
	}

	*//**
	 * 关闭任务调度器，调用此方法之后，将无法再添加新的任务
	 * 
	 * @throws SchedulerException
	 *//*
	public void shutdown() throws SchedulerException {
		scheduler.shutdown();
	}

	*//**
	 * 返回任务调度器是否启动
	 * @return
	 * @throws SchedulerException 
	 *//*
	public boolean isStarted() throws SchedulerException {
		return scheduler.isStarted();
	}
	
	
	*//**
	 * 暂停任务
	 * @throws SchedulerException
	 *//*
	public void pauseTask(String taskName) throws SchedulerException { 
		
		List<Task> tasks = taskDao.find("from Task where taskName = ?", taskName);
		if(tasks.size() > 0){
			Task task = tasks.get(0);
			task.setTaskStatus(Task.PAUSE);
			taskDao.update(task);
		}
		
        scheduler.pauseJob(taskName, "DEFAULT");
    }
	
	*//**
	 * 恢复任务
	 * @param taskName
	 * @throws SchedulerException 
	 *//*
	public void recoverTask(String taskName) throws SchedulerException{
		
		List<Task> tasks = taskDao.find("from Task where taskName = ?", taskName);
		if(tasks.size() > 0){
			Task task = tasks.get(0);
			task.setTaskStatus(Task.RUNNING);
			taskDao.update(task);
		}
		
		scheduler.resumeJob(taskName, "DEFAULT");
		//scheduler.triggerJob(taskName, "DEFAULT");
	}
	
	*//**
	 * 删除任务
	 * @param taskName
	 *//*
	public void deleteTask(String taskName){
		List<Task> tasks = taskDao.find("from Task where taskName = ?", taskName);
		if(tasks.size() > 0){
			Task task = tasks.get(0);
			taskDao.delete(task);
		}
		try {
			scheduler.deleteJob(taskName, "DEFAULT");
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
	}
	
	public TaskDao getTaskDao() {
		return taskDao;
	}*/

}
